﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace Sinacor.OMM.FileTail
{
    public class UserExperienceHelper
    {
        private const string CHR1013 = "\r\n";
        private const string LAST_USED_FILE_NAME_LIST = "TraceTailLastUsedFileList.txt";
        private const string LAST_USED_FILE_NAME = "TraceTailLastUsedFileName.txt";

        private const int MAX_LAST_USED_FILES = 20;

        Dictionary<string, string> _lastUsedFilesDict = new Dictionary<string, string>();

        private string _pathToSaveFiles;

        public UserExperienceHelper(string pathToSaveFiles)
        {
            _pathToSaveFiles = pathToSaveFiles + @"\";
        }


        public void AddLastUsedFile(string filename, string safeFileName)
        {

            if (!_lastUsedFilesDict.ContainsKey(filename))
            {
                File.AppendAllText(_pathToSaveFiles + LAST_USED_FILE_NAME_LIST, filename + CHR1013 + safeFileName + CHR1013);
                _lastUsedFilesDict.Add(filename, safeFileName);
            }
        }

        public Dictionary<string, string> GetLastUsedFiles()
        {

            int iniIdx = 0;

            if (File.Exists(_pathToSaveFiles + LAST_USED_FILE_NAME_LIST))
            {
                string[] files = File.ReadAllLines(_pathToSaveFiles + LAST_USED_FILE_NAME_LIST);

                if (files.Length > MAX_LAST_USED_FILES)
                {
                    File.Delete(_pathToSaveFiles + LAST_USED_FILE_NAME_LIST);
                    iniIdx = files.Length - MAX_LAST_USED_FILES;
                }

                _lastUsedFilesDict.Clear();
                for (int idx = iniIdx; idx < files.Length; idx++)
                {
                    if (!_lastUsedFilesDict.ContainsKey(files[idx]))
                        AddLastUsedFile(files[idx], files[++idx]);
                    else
                        idx++;
                }
            }

            return _lastUsedFilesDict;
        }

        public string GetSizeUnits(double size)
        {
            const int KBYTE = 1024;
            const int MBYTE = 1024 * 1024;
            const int GBYTE = 1024 * 1024 * 1024;

            if (size == 0)
                return string.Empty;

            if (size < KBYTE)
                return size + " bytes";

            if (size < MBYTE)
                return Math.Round((size / KBYTE),2).ToString() + " Kb";

            if (size < GBYTE)
                return Math.Round((size / MBYTE), 2).ToString() + " Mb";
            else
                return Math.Round((size / GBYTE), 2).ToString() + " Gb";

        }

        public void SaveLastUsedFile(string fileName)
        {
            try
            {
                if (File.Exists(_pathToSaveFiles + LAST_USED_FILE_NAME))
                    File.Delete(_pathToSaveFiles + LAST_USED_FILE_NAME);

                File.AppendAllText(_pathToSaveFiles + LAST_USED_FILE_NAME, fileName);
            }
            catch (Exception) { }
        }

        public string LoadLastUsedFile()
        {
            string ret = string.Empty;

            try
            {
                if (File.Exists(_pathToSaveFiles + LAST_USED_FILE_NAME))
                    ret = File.ReadAllText(_pathToSaveFiles + LAST_USED_FILE_NAME);
            }
            catch (Exception) { }

            return ret;
        }

        private int CalcNumOfWindows(ArrangeType arrangeType)
        {
            int ret = 0;

            switch (arrangeType)
            {
                case ArrangeType.Arrange2Horizontal:
                    ret = 2;
                    break;
                case ArrangeType.Arrange2Vertical :
                    ret = 2;
                    break;
                case ArrangeType.Arrange3Horizontal:
                    ret = 3;
                    break;
                case ArrangeType.Arrange3Vertical:
                    ret = 3;
                    break;
                case ArrangeType.Arrange2x2:
                    ret = 4;
                    break;
                case ArrangeType.Arrange3x2:
                    ret = 6;
                    break;
                default:
                    ret = 1;
                    break;
            }
            return ret;
        }

        public enum ArrangeType
        {
            Arrange2Vertical,
            Arrange2Horizontal,
            Arrange3Horizontal,
            Arrange3Vertical,
            Arrange2x2,
            Arrange3x2
        }
    }
}
